#include "CutsceneState.h"

#include "Game.h"
#include "MainMenuState.h"
#include "GamePlayState.h"

// SGD Wrappers
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"

// Initialize the scene
void CutsceneState::Enter()
{
	/* Setup Scene data */
	Scene = SCENE_NULL;	
	Image = -1;			
	Text = "";	
	Finished = false;		
	TimeRemaining = 0;


	X = Y = (float)(SpeedX = SpeedY = 0);

	if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(Game::GetInstance()->GetMenuMusic()) == true)
		CSGD_XAudio2::GetInstance()->MusicStopSong(Game::GetInstance()->GetMenuMusic());
}

// Shutdown
void CutsceneState::Exit()
{
	if( Image != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(Image);
		Image = -1;
	}
}

// Get input
bool CutsceneState::Input()
{
	if( CSGD_DirectInput::GetInstance()->JoystickButtonPressed( 0 ))
		TimeRemaining = 0;
	return true;
}

// Update the scene
void CutsceneState::Update(float elapsedTime)
{
	X += SpeedX * elapsedTime;
	Y += SpeedY * elapsedTime;

	TimeRemaining -= elapsedTime;
	if( TimeRemaining <= 0 )
	{
		TimeRemaining = 0;
		Finished = true;

		if( Scene == SCENE_BEGINNING )
		{
			RunCutscene(SCENE_BEGINNING2);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if( Scene == SCENE_RESEARCHLABSTORY )
		{
			RunCutscene(SCENE_RESEARCHLAB);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if( Scene == SCENE_RESEARCHLAB )
		{
			RunCutscene(SCENE_RESEARCHLAB2);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_POWERSTORY)
		{
			RunCutscene(SCENE_POWER);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_POWER)
		{
			RunCutscene(SCENE_POWER2);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_RSTORY)
		{
			RunCutscene(SCENE_R);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_R)
		{
			RunCutscene(SCENE_R2);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_MSTORY)
		{
			RunCutscene(SCENE_M);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_M)
		{
			RunCutscene(SCENE_M2);
			GamePlayState::GetInstance()->inCutscene = true;
		}
		else if(Scene == SCENE_VICTORY)
			GamePlayState::GetInstance()->ShipLevelStart();
		else if(Scene == SCENE_GAMEOVER)
		{
			//ObjectManager::GetInstance()->RemoveAllObjects();
			GamePlayState::GetInstance()->ShipLevelStart();
			//Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			//return;
		}
		else if(Scene == SCENE_GAMECOMPLETE)
		{
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return;
		}
	}
}

// Draw the scene
void CutsceneState::Render()
{
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	// Draw the image
	if( Image != -1 )
		CSGD_TextureManager::GetInstance()->Draw(Image,0,0);

	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	if( Image == -1 )
	{
		RECT r = {0,0,800,600};
		CSGD_Direct3D::GetInstance()->DrawRect(r,0,0,0);
	}

	// Draw the text
	Game::GetInstance()->DrawBitmapFont(Text.c_str(),(int)X,(int)Y,0.75f,D3DCOLOR_XRGB(0,100,200));
}

// Show a cutscene
void CutsceneState::RunCutscene(Cutscenes scene)
{

	// Unload the old image
	if( Image != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(Image);
		Image = -1;
	}

	Scene = scene;

	switch (scene)
	{
	case SCENE_BEGINNING:
		Image = -1;
		Text =  "Humanity is on the brink of destruction.\n\n\n\nIt is the year 40 EC.\n\nThe sun is about to explode.\n\nHumanity believes the Collective,\n\na race of superintelligent robots,\n\nhave the technology to save them.\n\n\n\nThe fate of humanity rests in the hands\n\nof their little robot ambassador, RoBHERT,\n\nwho has been sent to retrieve the\n\ntechnology by any means necessary.";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "De mensheid staat op de rand van vernietiging.\n\n\n\nHet is het jaar 40 EG.\n\nDe zon staat op ontploffen.\n\nDe mensheid gelooft dat de Collective,\n\neen ras van superintelligente robots,\n\nhebben de technologie om hen te redden.\n\n\n\nHet lot van de mensheid ligt in de handen\n\nvan de kleine robot ambassadeur, RoBHERT,\n\ndie is verzonden naar de map\n\ntechnologie op te halen met alle mogelijke middelen.";
		X = 80;
		Y = 650;
		SpeedX = 0;
		SpeedY = -35;
		TimeRemaining = 35.0f;
		Finished = false;
		break;
	case SCENE_BEGINNING2:
		Image = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/cutscene1.png"),D3DCOLOR_XRGB(0,255,0));
		Text = "RoBHERT";
		X = 350;
		Y = 400;
		SpeedX = SpeedY = 0;
		TimeRemaining = 5.0f;
		Finished = false;
		break;
	case SCENE_RESEARCHLABSTORY:
		Image = -1;
		Text = "After attempting to contact the Collective,\n\nRoBHERT was immediately fired upon.\n\nThis made it apparent that achieving\n\n the technology through diplomatic \n\nmeans was impossible.\n\n\n\nInstead, RoBHERT used his ship's scanners\n\n to locate the first piece of the device,\n\nwhich had been codenamed the SAVIOR.\n\nIt was located in a Collective Research Facility...";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Na een poging om het Collective contact op te nemen,\n\nRoBHERT werd onmiddellijk onder vuur genomen.\n\nDit maakte het duidelijk dat het bereiken van\n\nde technologie door middel van diplomatieke\n\nmiddelen onmogelijk was.\n\n\n\nplaats daarvan RoBHERT gebruikte zijn schip scanners\n\nom het eerste stuk van het toestel te lokaliseren,\n\ndat was de codenaam van de Heiland.\n\nhet is gelegen in een Collective Research Facility ...";
		X = 40;
		Y = 650;
		SpeedX = 0;
		SpeedY = -35;
		TimeRemaining = 30.0f;
		Finished = false;
		break;
	case SCENE_RESEARCHLAB:
		Image = -1;
		Text = "Chapter 1:";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 1:";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_RESEARCHLAB2:
		Image = -1;
		Text = "Chapter 1:\n\n\tThe Research Lab";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 1:\n\n\tHet Research Lab";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_POWERSTORY:
		Image = -1;
		Text = "With the device under his control,\n\nRoBHERT now heads to locate a power supply\n\nin a nearby Collective Power Plant";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 1:\n\n\tHet Research Lab";
		X = 40;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 20.0f;
		Finished = false;
		break;
	case SCENE_POWER:
		Image = -1;
		Text = "Chapter 2:";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 2:";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_POWER2:
		Image = -1;
		Text = "Chapter 2:\n\n\tThe Power Plant";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 2:\n\n\tDe Power Plant";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_MSTORY:
		Image = -1;
		Text = "The final piece of the SAVIOR device,\n\nwas hidden in a Collective Military Facility";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 1:\n\n\tHet Research Lab";
		X = 40;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 15.0f;
		Finished = false;
		break;
	case SCENE_M:
		Image = -1;
		Text = "Chapter 3:";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 3:";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_M2:
		Image = -1;
		Text = "Chapter 3:\n\n\tMilitary Facility";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 3:\n\n\tDe Power Plant";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
		case SCENE_RSTORY:
		Image = -1;
		Text = "Just as he was ready to return to Earth,\n\nRobhert is abducted by the Collective\n\nand sent to a recycling center\n\nto be destroyed.\n\n\n\nNow, he must escape";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 1:\n\n\tHet Research Lab";
		X = 40;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_R:
		Image = -1;
		Text = "Chapter 4:";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 3:";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_R2:
		Image = -1;
		Text = "Chapter 4:\n\n\tThe Recycling Center";
		if( Game::GetInstance()->GetLanguage() == false )
			Text = "Chapiter 3:\n\n\tDe Power Plant";
		X = 300;
		Y = 300;
		SpeedX = SpeedY = 0;
		TimeRemaining = 2.0f;
		Finished = false;
		break;
	case SCENE_GAMEOVER:
		Image = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/earth-exploding.jpg"),D3DCOLOR_XRGB(0,255,0));
		Text = "RoBHERT has failed his mission.\n\nHUMANITY HAS BEEN DESTROYED.";
		SpeedX = SpeedY = 0;
		X = 40;
		Y = 480;
		TimeRemaining = 20.0f;
		Finished = false;
		break;
	case SCENE_VICTORY:
		Image = -1;
		Text = "RoBHERT has collected a savior piece!";
		X = 100;
		Y = 100;
		SpeedX = SpeedY = 0;
		TimeRemaining = 20.0f;
		Finished = false;
		break;
	case SCENE_GAMECOMPLETE:
		Image = -1;
		Text = "Upon defeating the saw bot\n\nin the recycling center, Robhert\n\nmanages to escape to his ship and return\n\nsafely to Earth in time to\n\nactivate the SAVIOR and save humanity.\n\n\n\nThe End.";
		X = 40;
		Y = 650;
		SpeedX = 0;
		SpeedY = -35;
		TimeRemaining = 30.0f;
		Finished = false;
		break;
	default:
		Scene = SCENE_NULL;
		break;
	}
}

// Get the singleton
CutsceneState* CutsceneState::GetInstance()
{
	static CutsceneState instance;
	return &instance;
}